package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.entity.Dj;

import java.util.List;

public class DjHorAdapter extends RecyclerView.Adapter<DjHorAdapter.DjHorHolder>{
    private List<Dj.DjRadiosBean> djRadiosBeanList;
    private Context context;

    public void setData(List<Dj.DjRadiosBean> djRadiosBeanList,Context context){
        this.context = context;
        this.djRadiosBeanList = djRadiosBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DjHorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_hor,parent,false);
        return new DjHorHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DjHorHolder holder, int position) {
        Dj.DjRadiosBean djRadiosBean =  djRadiosBeanList.get(position);
        if(djRadiosBean.getRcmdtext()==null) {
            holder.tv_name.setText("无人声电台");
        }
        else{
            holder.tv_name.setText(djRadiosBean.getRcmdtext());
        }

        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(djRadiosBean.getPicUrl())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options)
                .into(holder.img_recycler_hor);
    }

    @Override
    public int getItemCount() {
        return djRadiosBeanList==null?0:djRadiosBeanList.size();
    }

    class DjHorHolder extends RecyclerView.ViewHolder {
        ImageView img_recycler_hor;
        TextView tv_name;
        public DjHorHolder(@NonNull View itemView) {
            super(itemView);
            img_recycler_hor = itemView.findViewById(R.id.img_recycler_hor);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }
}
